package com.sample.library.portlet.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.sample.library.model.Book;

public class BookValidator implements Validator {

	@Override
	public boolean supports(Class clazz) {
		return Book.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object object, Errors errors) {
		Book book = (Book)object;
		validateTitle(book, errors);
	}

	public void validateTitle(Book book, Errors errors) {
		ValidationUtils.rejectIfEmpty(errors, "title", "TITLE_REQUIRED", "Title of book is required.");
	}
}
